<?php


namespace app\admin\model\member;


use core\basic\Model;

class ActivationModel extends  Model
{


    // 获取激活码列表
    public function getList()
    {
        $field = array(
            'a.*',
            'u.username',

        );
        $join = array(
            'ay_member u',
            'a.user_id=u.id',
            'LEFT'
        );
        return parent::table('ay_activation a')
            ->field($field)
            ->join($join)
            ->order('a.status asc')
            ->page()
            ->select();
    }
    // 激活码字段
    public function getFields()
    {
		$field = array(
			'a.*',
			'u.username',

		);
		$join = array(
			'ay_member u',
			'a.user_id=u.id',
			'LEFT'
		);
        return parent::table('ay_activation')
			->field($field)
			->join($join)
			->where('status=1')
            ->order('sorting')
            ->select();
    }

    // 获取激活码详情
    public function getMember($id)
    {
        $field = array(
            'a.*',
            'u.username',

        );
        $join = array(
            'ay_member u',
            'a.user_id=u.id',
            'LEFT'
        );
        return parent::table('ay_activation a')->field($field)
            ->join($join)
            ->where("a.user_id=$id")
            ->find();
    }
    // 获取推荐人信息
    public function  getTreeCate($where)
    {

        return parent::table('ay_activation')->where($where)->field('username')->find();

    }
    // 查询激活码
    public function checkCode($where)
    {
        return parent::table('ay_activation')->where($where)->select();
    }
	// 查询一条激活码
	public function checkCodefind($where)
	{
		return parent::table('ay_activation')->where($where)->find();
	}
    // 添加激活码
    public function addActivation(array $data)
    {
        return parent::table('ay_activation')->insert($data);
    }
    // 查找激活码
    public function findCode($field, $keyword)
    {
        $fields = array(
            'a.*',
            'u.username',

        );
        $join = array(
            'ay_member u',
            'a.user_id=u.id',
            'LEFT'
        );

        return parent::table('ay_activation a')->field($fields)
            ->join($join)
            ->like($field, $keyword)
            ->order('a.id desc')
            ->page()
            ->select();
    }
    // 批量删除激活码
    public function delActivationList($ids)
    {
        return parent::table('ay_activation')->delete($ids);
    }
    // 删除激活码
    public function delActivation($id)
    {
        return parent::table('ay_activation')->where("id=$id")->delete();
    }
 //查询当前用所拥有未使用的激活码
    public function  getCurrent($usermobile){
        return parent::table('ay_activation')->where(['usermobile'=>$usermobile,'status'=>1])->field('id')->find();
    }
    //修改激活码状态
    public function   modCurrent($id,$data){
        return parent::table('ay_activation')->where("id=$id")->update($data);
    }
//    // 获取当前激活码列表
    public function GetActivation($where,$limit)
    {
        return parent::table('ay_activation ')
            ->where($where)
              ->limit($limit)
            ->select();
    }
    //获取激活码总数
    public function GetCount($where){
		return parent::table('ay_activation ')
			->where($where)
			->count();
	}

}
